package logic

import (
	"context"
	"fmt"
	"patient/patientrpc/models/mysql"

	"patient/patientrpc/internal/svc"
	"patient/patientrpc/patientrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *patientrpc.UserLoginRequest) (*patientrpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line

	var users mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Find(&users)

	if users.ID == 0 {
		return nil, fmt.Errorf("账号不存在不允许登录,请前去注册")
	}
	if users.Password != in.Password {
		return nil, fmt.Errorf("密码错误,请重新输入")
	}
	return &patientrpc.UserLoginResponse{
		Id: int64(users.ID),
	}, nil
}
